package com.easybuy.controller;


import com.easybuy.Tools.Page;
import com.easybuy.pojo.Category;
import com.easybuy.pojo.News;
import com.easybuy.pojo.Product;
import com.easybuy.service.CategoryService.CategoryService;
import com.easybuy.service.NewsSerivce.NewsSerivce;
import com.easybuy.service.ProductService.ProductService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
//主页功能
public class IndexController {
    @Autowired
    private CategoryService categoryService;
    @Autowired
    private NewsSerivce newsSerivce;
    @Autowired
    private ProductService productService;

    @RequestMapping("/selectSanCeng")
    @ResponseBody
    //三层分类
    public List<Category> selectSanCeng(){
        System.out.println("selectSanCeng");
        return categoryService.selectSanCeng();
    }


    @RequestMapping("/selectNews")
    @ResponseBody
    //主页新闻展示（5条）
    public List<News> selectNews(Page page){
        System.out.println("selectNews==========================================");
        return newsSerivce.newsList(page).getList();
    }


    @RequestMapping("/indexProducts")
    @ResponseBody
    //主页商品展示，查询每个大块下的产品和其分类结构
    public Map indexProducts(@RequestParam Integer id){
        System.out.println("indexProducts=======================================");
        List productList=new ArrayList();
        Map allMap=new HashMap();
        Map map=new HashMap();
        Page page=new Page();
        page.setPageNow(1);
        page.setPageSize(6);
        map.put("id",id);
        map.put("page",page);
        productList=productService.selectProducts(map).getList();
        Category categoryL1=categoryService.selectById(id);
        List<Category> categoryL2=categoryService.selectByParentId(id);
        List<Category> categoryL3=new ArrayList<>();
        for(Category ca:categoryL2){
            List<Category> list=categoryService.selectByParentId(ca.getId());
            categoryL3.addAll(list);
        }
        allMap.put("categoryL1",categoryL1);
        allMap.put("categoryL2",categoryL2);
        allMap.put("categoryL3",categoryL3);
        allMap.put("productList",productList);
        return allMap;
    }
}
